package isp.apps.example.shoppingadmin.action;

import com.opensymphony.xwork.ActionSupport;

import isp.apps.example.shoppingcart.domain.ShoppingCartValidator;
import isp.apps.example.shoppingcart.domain.ShippingOption;
import isp.apps.example.shoppingcart.persistence.ShippingOptionsRepository;
import isp.apps.example.shoppingcart.persistence.factory.ShippingOptionsPersistenceFactory;

import java.math.BigDecimal;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ShippingOptionsUpdatesAction
  extends ActionSupport
{
  private String updatedServiceName, updatedCompanyName, updatedTime, updatedValid, updatedRate;
  private List allShippingOptions;
  private List validShippingOptions;
  private Map shippingCompanyNameMap;
  private Map shippingServiceNameMap;
  private Map shippingRateMap;
  private Map shippingTimeMap;
  private Map shippingValidMap;

  public ShippingOptionsUpdatesAction()
  {
  }

  public String execute()
  {
    validate();
    if (hasActionErrors())
       return SUCCESS;
    writeToRepository();
    return SUCCESS;
  }

  public void writeToRepository()
  {
    ShippingOptionsRepository sor = ShippingOptionsPersistenceFactory.getShippingOptionsRepository();
    ShippingOption so;
    for (Iterator i = allShippingOptions.iterator(); i.hasNext();)
    {
       so = (ShippingOption)i.next();
       String id = so.getId()+"";
       so.setCompanyName((String)shippingCompanyNameMap.get(id));
       so.setServiceName((String)shippingServiceNameMap.get(id));
       so.setRate(new BigDecimal((String)shippingRateMap.get(id)));
       so.setTime(new Integer((String)shippingTimeMap.get(id)));
       so.setValid(new Integer((String)shippingValidMap.get(id)));
       sor.saveShippingOption(so);
    }
  }

  public void validate()
  {
    for (Iterator i = allShippingOptions.iterator(); i.hasNext();)
    {
      ShippingOption so = (ShippingOption)i.next();
      Integer id = so.getId();
      if (!(shippingCompanyNameMap.containsKey(id) && shippingServiceNameMap.containsKey(id) && shippingRateMap.containsKey(id))
      && shippingTimeMap.containsKey(id)&& shippingValidMap.containsKey(id))
      {
          addActionError("Invalid POST method or missing / corrupted items list.");
          return;
      }
      ShoppingCartValidator scv = new ShoppingCartValidator();
      
      updatedCompanyName = scv.checkText((String)shippingCompanyNameMap.get(id+""));
      updatedServiceName = scv.checkText((String)shippingServiceNameMap.get(id+""));
      updatedRate = scv.checkCurrency((String)shippingRateMap.get(id+""));
      updatedTime = scv.checkNumber((String)shippingTimeMap.get(id+""));
      updatedValid = scv.checkBoolean((String)shippingValidMap.get(id+""));

      if ((updatedCompanyName) == null)
      {
         addActionError("Must enter an valid company name to update shipping option: " + so.getServiceName()+ " - " + so.getServiceName() );
         return;
      }
      if (updatedServiceName == null)
      {
         addActionError("Must enter an valid service name to update shipping option: " + so.getServiceName()+ " - " + so.getServiceName() );
         return;
      }
      if (updatedRate == null)
      {
         addActionError("Must enter an valid rate to update shipping option: " + so.getServiceName()+ " - " + so.getServiceName() );
         return;
      }
      if (updatedTime == null)
      {
         addActionError("Must enter an valid time to update shipping option:" + so.getServiceName()+ " - " + so.getServiceName() );
         return;
      }
      if (updatedValid == null)
      {
         addActionError("Must specify valid (0 or 1) to to update shipping option: " + so.getServiceName()+ " - " + so.getServiceName() );
         return;
      }
      
      shippingCompanyNameMap.put(id+"",updatedCompanyName);
      shippingServiceNameMap.put(id+"",updatedServiceName);
      shippingRateMap.put(id+"",updatedRate);
      shippingTimeMap.put(id+"",updatedTime);
      shippingValidMap.put(id+"",updatedValid);
    }
  }

  public Map getShippingCompanyNameMap()
  {
    if (shippingCompanyNameMap == null)
      shippingCompanyNameMap = new HashMap();
    return shippingCompanyNameMap;
  }

  public Map getShippingServiceNameMap()
  {
    if (shippingServiceNameMap == null)
      shippingServiceNameMap = new HashMap();
    return shippingServiceNameMap;
  }

  public Map getShippingRateMap()
  {
    if (shippingRateMap == null)
      shippingRateMap = new HashMap();
    return shippingRateMap;
  }

  public Map getShippingTimeMap()
  {
    if (shippingTimeMap == null)
      shippingTimeMap = new HashMap();
    return shippingTimeMap;
  }

  public Map getShippingValidMap()
  {
    if (shippingValidMap == null)
      shippingValidMap = new HashMap();
    return shippingValidMap;
  }

  public void setAllShippingOptions(List allShippingOptions)
  {
    this.allShippingOptions = allShippingOptions;
  }

  public List getAllShippingOptions()
  {
    return allShippingOptions;
  }

  public void setValidShippingOptions(List validShippingOptions)
  {
    this.validShippingOptions = validShippingOptions;
  }

  public List getValidShippingOptions()
  {
    return validShippingOptions;
  }
}
